package leecodeHot100.hash;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SubStringStr {
    public static void main(String[] args) {
        String s1 = "abo";
        String s2 = "zidbaooo";
        fun1(s1, s2);
        //滑动窗口
        System.out.println(fun2(s1, s2));
    }

    private static boolean fun2(String s1, String s2) {
        int n = s1.length(), m = s2.length();
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        for (int i = 0; i < n; ++i) {
            ++cnt1[s1.charAt(i) - 'a'];
            ++cnt2[s2.charAt(i) - 'a'];
        }
        if (Arrays.equals(cnt1, cnt2)) {
            return true;
        }
        for (int i=n;i<m;i++){
            ++cnt2[s2.charAt(i) - 'a'];
            --cnt2[s2.charAt(i - n) - 'a'];
            if (Arrays.equals(cnt1, cnt2)) {
                return true;
            }
        }
        return false;
    }

    private static void fun1(String s1, String s2) {
        //把s1长度的字符串模板（ba->ab）存入set,再在set中查找是否出现
        Set<String> set = new HashSet<>();
        char[] array1 = s1.toCharArray();
        Arrays.sort(array1);
        String str = new String(array1);
        char[] array = s2.toCharArray();
        for(int i=0;i<array.length-array1.length+1;i++){
            String substring = s2.substring(i, i + array1.length);
            char[] array2 = substring.toCharArray();
            Arrays.sort(array2);
            set.add(new String(array2));
        }
        System.out.println(set.contains(str));
    }
}
